package com.yunnuo.system.comm.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yunnuo.system.comm.util.Empty;

import javax.persistence.*;
import java.util.Set;

/**
 * 树结构数据库表映射对象持久化类
 *
 * @author wangliang@hotmail.com
 * @date 2012-3-19 下午03:10:14
 * @description 在继承ObjectEntity类的基础上添加了父对象（parent）、子对象集合(children)两个属性。
 */
@SuppressWarnings("unchecked")
@MappedSuperclass
public class TreeEntity<T extends TreeEntity> extends ObjectEntity {

    private static final long serialVersionUID = 1L;
    private T parent;
    private Set<T> children;



    /**
     * 继承并添加了对父类的更新。
     * @param vo
     */
    public void setVO(TreeEntity vo){
        super.setVO(vo);
        if(Empty.isEmpty(this.parent)||!this.parent.equals(vo.getParent())){
            this.parent = (T)vo.getParent();
        }
    }

    /**
     * @return the parent
     */
    @JsonIgnore
    @ManyToOne()
    @JoinColumn(name = "parent_id")
    public T getParent() {
        return parent;
    }

    /**
     * @param parent
     *            the parent to set
     */
    public void setParent(T parent) {
        this.parent = parent;
    }

    /**
     * @return the childen
     */
    @JsonIgnore
    @OneToMany(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "parent_id")
    public Set<T> getChildren() {
        return children;
    }

    /**
     * @param children
     *    the childen to set
     */
    public void setChildren(Set<T> children) {
        this.children = children;
    }
}
